package com.cos.util;

import java.io.UnsupportedEncodingException;

/**
 * 字符串相关方法
 *
 */
public class StringUtil {

	/**
	 * 将以逗号分隔的字符串转换成字符串数组
	 * @param valStr
	 * @return String[]
	 */
	public static String[] StrList(String valStr){
	    int i = 0;
	    String TempStr = valStr;
	    String[] returnStr = new String[valStr.length() + 1 - TempStr.replace(",", "").length()];
	    valStr = valStr + ",";
	    while (valStr.indexOf(',') > 0)
	    {
	        returnStr[i] = valStr.substring(0, valStr.indexOf(','));
	        valStr = valStr.substring(valStr.indexOf(',')+1 , valStr.length());
	        
	        i++;
	    }
	    return returnStr;
	}
	
	/**
	 * 更改参数的编码为UTF8
	 * @param str
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public static String codeUtf(String str) throws UnsupportedEncodingException{
		str = new String(str.getBytes("ISO-8859-1"), "UTF-8");
		return str;
	}

	/**
	 * 把pagedata里面所有的数据都换个编码
	 * @param pd
	 * @throws UnsupportedEncodingException
	 */
	public static void codePageData(PageData pd) throws UnsupportedEncodingException {
		@SuppressWarnings("rawtypes")
		java.util.Iterator it = pd.entrySet().iterator();
		while(it.hasNext()){
			@SuppressWarnings("rawtypes")
			java.util.Map.Entry entry = (java.util.Map.Entry)it.next();
			String key = (String) entry.getKey() ;//返回与此项对应的键
			Object str = entry.getValue() ;//返回与此项对应的值
			str = new String(((String) str).getBytes("ISO-8859-1"), "UTF-8");
			pd.put(key, str);
		}
	}
	
	/**
	 * 不是空
	 * @param str
	 * @return
	 */
	public static boolean isNotEmpty(String str){
		if((str!=null)&&!"".equals(str.trim())){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 拼接模糊查询
	 * @param str
	 * @return
	 */
	public static String formatLike(String str){
		if(isNotEmpty(str)){
			return "%"+str+"%";
		}else{
			return null;
		}
	}
}
